package com.highcom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.highcom.pojo.Role;
import com.highcom.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/jueseguanli")
    public ModelAndView findAllRoles(@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo) {
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<Role> roleList = roleService.findAllRoles();
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        m.addObject("roleInfo", pageInfo);
        m.setViewName("/files/jueseguanli.jsp");
        return m;
    }

    @RequestMapping("/delRoleById")
    public String delRoleById(int id) {
        System.out.println(id);
        roleService.delRoleById(id);
        return "redirect:/jueseguanli";
    }

    @RequestMapping("/delRolesByids")
    public String delRolesByIds(int[] ids){
        roleService.delRolesByIds(ids);
        return "redirect:/jueseguanli";
    }

    @RequestMapping("/findRoleById")
    public ModelAndView findRoleById(int id) {
        ModelAndView m = new ModelAndView();
        Role role = roleService.findRoleById(id);
        m.addObject("roleInfo", role);
        m.setViewName("/files/juesemodify.jsp");
        return m;
    }

    @RequestMapping("/modifyRole")
    public String modifyRole(Role role) {
        System.out.println(role.getId());
        roleService.modifyRole(role);
        return "redirect:/jueseguanli";
    }

    @RequestMapping("/toaddRole")
    public String toAddRole() {
        return "/files/jueseadd.jsp";
    }

    @RequestMapping("/addRole")
    public String addRole(Role role) {
        roleService.addRole(role);
        return "redirect:/jueseguanli";
    }

    @RequestMapping("/queryRole")
    public ModelAndView findRolesLikeQuery(Role role,@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo) {
        System.out.println(role.toString());
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<Role> rolesLikeQuery = roleService.findRolesLikeQuery(role);
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Role> pageInfo = new PageInfo<>(rolesLikeQuery);
        m.addObject("roleInfo", pageInfo);
        m.setViewName("/files/jueseguanli.jsp");
        return m;
    }
}
